Ext.define('SRMT.controller.project-explorer.ProjectExplorer', {
	extend: 'Ext.app.Controller',
	
	views: ['project-explorer.ProjectExplorer', 'project-explorer.ContextMenu'],
	
	stores: ['SRMT.store.ProjectExplorer'],
	
	refs: [{
		ref: 'projectExplorer',
		selector: 'srmtProjectExplorer'
	},{
		ref: 'contextMenu',
		selector: 'srmtProjectExplorerContextMenu'
	}],
	
	init: function() {
		Ext.create('SRMT.view.project-explorer.ContextMenu');
		this.control({
			'srmtProjectExplorer': {
				render: this.onRender,
				itemclick: this.onItemClick,
				itemdblclick: this.onItemDblClick,
				itemcontextmenu: this.onItemContextMenu,
				containercontextmenu: this.onContainerContextMenu
			}
		});
	},
	
	onRender: function() {
		
	},
	
	onItemClick: function(view, record, item, index, e, eOpts) {
		
	},
	
	onItemDblClick: function(view, record, item, index, e, eOpts) {
		
		if(record.data.id.substring(0,1) == 'p'){
			var tab = Ext.create('SRMT.view.project-board.ProjectBoard', {title: record.data.text, itemId: record.data.id, closable: true});
			var projectId = parseInt(record.data.id.substr(2, record.data.id.length-2));
			this.getController('SRMT.controller.artifact-list.ArtifactList').getArtifactListByProject(projectId);
		} else if(record.data.id.substring(0,1) == 'f'){
			var tab = Ext.create('SRMT.view.project-board.ProjectBoard', {title: record.parentNode.data.text, itemId: record.parentNode.data.id, closable: true});
			var folderId = parseInt(record.data.id.substr(2, record.data.id.length-2));
			this.getController('SRMT.controller.artifact-list.ArtifactList').getArtifactListByFolder(folderId);
		}
		
		this.getController('SRMT.controller.Viewport').addTabToCenterPanel(tab);
	},
	
	onItemContextMenu: function(view, record, item, index, e, eOpts ) {
		e.preventDefault();
		this.getContextMenu().record = record
//		console.log(this.getContextMenu().record);
		contextMenuController = this.getController('project-explorer.ContextMenu');
		if(record.data.id.substring(0,1) == 'p'){
			contextMenuController.setProjectContextMenu();
		} else if(record.data.id.substring(0,1) == 'f') {
			contextMenuController.setFolderContextMenu();
		} else if(record.data.id.substring(0,1) == 'r') {
			contextMenuController.setContainerContextMenu();
		}
		
		contextMenuController.showAt(e.getX(), e.getY());
	},
	
	onContainerContextMenu: function(view, e, eOpts ) {
		e.preventDefault();
		contextMenuController = this.getController('project-explorer.ContextMenu');
		contextMenuController.setContainerContextMenu();
		contextMenuController.showAt(e.getX(), e.getY());
	}
	
//	updateView: function() {
//		this.getStore('SRMT.store.ProjectExplorer').load();
//	}
	
//	setUnvisible: function() {
//		this.getProjectExplorer().setVisible(false);
//	},
//	
//	setVisible: function() {
//		this.getProjectExplorer().setVisible(true);
//	}
});